-
Notifications
You must be signed in to change notification settings - Fork 6
total_seconds: Fix precision problem #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Since CircuitPython floats have very small precision, the "total_seconds" method was not very useful for large timedeltas. Instead of always returning a float, use pure-integer arithmetic (and return an int) if either: * the length of time is big (1<<21 is 2097152 seconds or about 24 days) * the number of microseconds is zero Otherwise, for small values with nonzero microseconds, use floating-point math. The cut-off point was chosen because in CircuitPython float arithmetic, 2097151.0+0.5 is different from 2097151.0, but 2097152.0+0.5 and 2097152.0 are *the same float*.
b1f9f0a
to
13f9332
Compare
brentru
approved these changes
Mar 26, 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm - thanks jepler!
adafruit-adabot
added a commit
to adafruit/Adafruit_CircuitPython_Bundle
that referenced
this pull request
Apr 7, 2021
Updating https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground to 4.3.3 from 4.3.2: > Merge pull request adafruit/Adafruit_CircuitPython_CircuitPlayground#104 from kattni/main-fix > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_DHT to 3.6.0 from 3.5.8: > Merge pull request adafruit/Adafruit_CircuitPython_DHT#68 from yeyeto2788/master > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_FocalTouch to 1.2.7 from 1.2.6: > Merge pull request adafruit/Adafruit_CircuitPython_FocalTouch#20 from adafruit/linting > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_RockBlock to 1.3.3 from 1.3.2: > Merge pull request adafruit/Adafruit_CircuitPython_RockBlock#21 from adafruit/linting > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 1.9.3 from 1.9.2: > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#32 from adafruit/linting > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO to 5.2.4 from 5.2.3: > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#68 from brentru/validate-feed-keys-used > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_AVRprog to 1.3.7 from 1.3.6: > Merge pull request adafruit/Adafruit_CircuitPython_AVRprog#21 from adafruit/linting > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_BitmapSaver to 1.1.6 from 1.1.5: > Merge pull request adafruit/Adafruit_CircuitPython_BitmapSaver#14 from kmatch98/patch-2 > "Increase duplicate code check threshold " > Merge pull request adafruit/Adafruit_CircuitPython_BitmapSaver#17 from FoamyGuy/pylintrc Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE_Radio to 0.3.5 from 0.3.4: > Merge pull request adafruit/Adafruit_CircuitPython_BLE_Radio#15 from adafruit/linting > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_BoardTest to 1.2.6 from 1.2.5: > Merge pull request adafruit/Adafruit_CircuitPython_BoardTest#15 from adafruit/linting > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_datetime to 1.1.3 from 1.1.2: > Merge pull request adafruit/Adafruit_CircuitPython_datetime#8 from adafruit/jepler-integer-total_seconds > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_Display_Text to 2.18.3 from 2.18.2: > Merge pull request adafruit/Adafruit_CircuitPython_Display_Text#150 from jposada202020/advanced_example > Merge pull request adafruit/Adafruit_CircuitPython_Display_Text#149 from jposada202020/optimizing_getting_font_info Updating https://github.com/adafruit/Adafruit_CircuitPython_RGBLED to 1.1.5 from 1.1.4: > Merge pull request adafruit/Adafruit_CircuitPython_RGBLED#15 from adafruit/dherrada-patch-1 > "Increase duplicate code check threshold " Updating https://github.com/adafruit/Adafruit_CircuitPython_RSA to 1.2.4 from 1.2.3: > Merge pull request adafruit/Adafruit_CircuitPython_RSA#18 from adafruit/linting > "Increase duplicate code check threshold "
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Since CircuitPython floats have very small precision, the "total_seconds" method was not very useful for large timedeltas.
Instead of always returning a float, use pure-integer arithmetic (and return an int) if either:
1<<21
is 2097152 seconds or about 24 days)Otherwise, for small values with nonzero microseconds, use floating-point math.
The cut-off point was chosen because in CircuitPython float arithmetic,
2097151.0+0.5
is different from2097151.0
, but2097152.0+0.5
and2097152.0
are the same float.